import { Injectable } from '@angular/core';
import { HttpService } from '../../../core/http.service';
import { CommHttpService } from '../../../core/comm.http.service';
import { SERVERS_NAME } from '../../../config/config';
import { Observable } from 'rxjs';
import { Response } from '../../../core/domain/response';
import { PageEntity } from '../../../core/domain/page.entity';
import { PaginationUtils } from '../../../utils/paginationUtils';
import { FilterEnum } from '../../../core/enum/filter.enum';

@Injectable()
export class DeviceManageService extends CommHttpService {
  protected preUrl = `${SERVERS_NAME.SECURITY_SERVER}sys/equipment/`;
  // 标识查询地址
  identifiesUrl = `${SERVERS_NAME.SECURITY_SERVER}sys/dictionary/listByType/`;
  deviceType = `${SERVERS_NAME.SECURITY_SERVER}sys/dictionary/listFilter/`;
  // 标识负责人地址
  userUrl = `${SERVERS_NAME.SECURITY_SERVER}sys/teacher/list`;

  constructor(protected http: HttpService) {
    super(http);
  }

  /**
   *查询设备标识
   */
  deviceIdentifiesList(): Observable<Response> {
    return this.http.post({}, this.identifiesUrl + 'device-identity');
  }

  /**
   *查询设备标识
   */
  deviceTypeList(): Observable<Response> {
    return this.http.post({}, this.deviceType + 'device-type');
  }

  /**
   *查询负责人
   */
  userList(): Observable<Response> {
    return this.http.post({}, this.userUrl);
  }

  /**
   * 删除设备
   */
  deviceDelete(ids): Observable<Response> {
    return this.http.post(ids, this.preUrl + 'delete');
  }

  /**
   * 获取房间树
   */
  treeWithRoom(): Observable<Response> {
    return this.http.post({}, `${SERVERS_NAME.SECURITY_SERVER}sys/floor/treeWithRoom`);
  }

  /**
   *
   * @param  list 数据列表
   * @param keyName 关键字名称
   * @param keyList 关键字值
   * @param valueName 需返回字段名称
   * @returns 返回显示数据
   */
  showNameBykeyList(list: any[], keyName: string, valueName: string, keyList: string[]): string[] {
    const showArr = [];
    keyList.forEach(key => {
      list.forEach(item => {
        if (item[keyName] !== key) {
          return;
        } else {
          showArr.push(item[valueName]);
        }
      });
    });
    return showArr;
  }

  /**
   * 分页配备字段
   * @param  list 关键字匹配值
   * @param pageInfo 分页条件
   */
  pageFarm(pageInfo: PageEntity, list: Array<{ name?: string, value?: string }>): void {
    list.forEach(item => {
      if (!item.value) {
        delete pageInfo.filters[item.name];
        return;
      } else {
        PaginationUtils.filter(pageInfo, item.name, item.value, FilterEnum.CONTAINS);
      }
    });
  }


  /**
   * 导入Excel
   *
   * @param fileId Excel文件id
   * @returns  返回值
   */
  importExcel(data: any) {
    return this.http.postFile(data, `${SERVERS_NAME.SECURITY_SERVER}` + 'sys/equipment/equipmentImport');
  }


}
